package com.efast.cafe.portal.service.base.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.base.BaseCompanyInfoBean;
import com.efast.cafe.portal.bean.company.PortalCompanyUserBean;
import com.efast.cafe.portal.dao.base.BaseCompanyInfoDao;
import com.efast.cafe.portal.entity.base.BaseCompanyInfo;
import com.efast.cafe.portal.entity.common.PortalAttachmentForm;
import com.efast.cafe.portal.service.base.IBaseCompanyInfoService;
import com.efast.cafe.portal.service.common.IPortalAttachmentService;
import com.efast.cafe.portal.service.company.IPortalCompanyUserService;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
/******************************************************************************
 * @File name   :      BaseCompanyInfoServiceImpl.java
 *
 * @Author      :      fxm
 *
 * @Date        :      2016-6-27
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-6-27 上午10:13:17        fxm     1.0            Initial Version
 *****************************************************************************/
@Service
@Transactional
public class BaseCompanyInfoServiceImpl extends CafeServiceImpl<BaseCompanyInfo> implements IBaseCompanyInfoService{
	
	
	private BaseCompanyInfoDao baseCompanyInfoDao;
	/*private IBaseCompanyInfoDao baseCompanyInfoDao;*/
	
	@Autowired
	private IPortalAttachmentService portalAttachmentService;
	@Autowired
	private IPortalCompanyUserService portalCompanyUserService;
	
	@Qualifier("jdbcDao")
	@Autowired
	private JdbcDao JdbcDao;
	
	@Autowired
	public void setBaseCompanyInfoDao(BaseCompanyInfoDao baseCompanyInfoDao) {
		this.baseCompanyInfoDao = baseCompanyInfoDao;
	}
	
	

	/**
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2016-6-27
	 * @see org.eae.eldf.portal.remote.base.BaseCompanyInfoService#findById(java.lang.Long)
	**/
	@Override
	public BaseCompanyInfo findById(Long id) {
		return JdbcDao.get(BaseCompanyInfo.class, id);
	}

	/**
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2016-6-27
	 * @see org.eae.eldf.portal.remote.base.BaseCompanyInfoService#updateCompanyInfo(org.eae.eldf.portal.po.base.BaseCompanyInfo)
	**/
	@Override
	public void updateCompanyInfo(BaseCompanyInfoBean portalCompanyInfo,List<PortalAttachmentForm> attachments) {
		if (portalCompanyInfo != null && portalCompanyInfo.getBaseCompanyInfoId() != null) {
//			JdbcDao.update(Criteria.create(BaseCompanyInfo.class)
//					//.set("companyName", portalCompanyInfo.getCompanyName())
//					.set("companyAddr", portalCompanyInfo.getCompanyAddr())
//					.set("companyRemark", portalCompanyInfo.getCompanyRemark())
//					.set("generateFormId", portalCompanyInfo.getGenerateFormId())
//					
//					.set("lastUpdateUser", SpringWebUtils.getRequestUser()).set("lastUpdateTime", new Date())
//
//					.where("baseCompanyInfoId", new Object[] { portalCompanyInfo.getBaseCompanyInfoId() })
//					.and("companyCode", new Object[] { SpringWebUtils.getRequestCompany() }));
			portalCompanyInfo.setCompanyCode(SpringWebUtils.getRequestCompany());
			baseCompanyInfoDao.updateCompany(portalCompanyInfo);
			if(portalCompanyInfo.getGenerateFormId()!=null && attachments!=null && attachments.size()>0){
				portalAttachmentService.saveAttachmentForm(portalCompanyInfo.getGenerateFormId(), attachments);
			}
			//portalAttachmentService.saveAttachmentForm(portalCompanyInfo.getGenerateFormId(), attachments.getAttachments());
		} else {
			throw new ServiceException("msg.portal.company.Info.serviceException.data");
		}
		
	}

	/**
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2016-6-27
	 * @see org.eae.eldf.portal.remote.base.BaseCompanyInfoService#queryList()
	**/
	@Override
	public List<BaseCompanyInfo> queryList() {
		return JdbcDao.queryList(Criteria.create(BaseCompanyInfo.class).where("isEnable", new Object[]{1}));
	}

	/**
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2016-6-27
	 * @see org.eae.eldf.portal.remote.base.BaseCompanyInfoService#queryByCode()
	**/
	@Override
	public BaseCompanyInfoBean queryByCode() {
//		Criteria criteria = Criteria.create(BaseCompanyInfo.class).where("isEnable", new Object[] { 1 })
//				.and("companyCode", Criteria.FIELD_OPERATOR_EQ, new Object[] { SpringWebUtils.getRequestCompany() });
//		List<BaseCompanyInfo> list = JdbcDao.queryList(criteria);
		BaseCompanyInfoBean companyInfo=baseCompanyInfoDao.queryByCompanyCode(SpringWebUtils.getRequestCompany());
		
		return companyInfo;
	}
	
	public BaseCompanyInfoBean queryByCode(String companyCode) {
		return baseCompanyInfoDao.queryByCompanyCode(companyCode);
	}

	@Override
	public List<BaseCompanyInfo> queryCompanys(String i18nKey) {
		String companyCode=SpringWebUtils.getRequestCompany();
		String username=SpringWebUtils.getRequestUser();
		PortalCompanyUserBean userInfo= portalCompanyUserService.queryUserInfo(companyCode, username);
		if(userInfo.getCompanyType()==1){
			companyCode=null;
		}
		return baseCompanyInfoDao.queryCompanys(i18nKey,companyCode);
	}
	

}
